﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Telerik.Windows.Controls;

namespace JustinAngel.MindwaveSensor.WP8.TestApp
{
    public partial class TelerikGagues : PhoneApplicationPage
    {
        public TelerikGagues()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (Mindwave.Current.IsDataValid)
            {
                SetGaguesValueTo(Mindwave.Current.CurrentValue);
            }

            Mindwave.Current.CurrentValueChanged += Current_CurrentValueChanged;

            base.OnNavigatedTo(e);
        }


        void Current_CurrentValueChanged(object sender, MindwaveReadingEventArgs e)
        {
            if (e.SensorReading.IsDataReliable)
            {
                SetGaguesValueTo(e.SensorReading);
            }
        }

        private void SetGaguesValueTo(MindwaveReading mindwaveReading)
        {
            ((SegmentedGaugeIndicator)gaugeAttention.Indicators[0]).Value = mindwaveReading.eSenseAttention;
            ((SegmentedGaugeIndicator)gaugeMeditation.Indicators[0]).Value = mindwaveReading.eSenseMeditation;

            txtIsAware.Text = mindwaveReading.eSenseAttention >= 70 ? "Yes" : "No";
            txtIsMeditative.Text = mindwaveReading.eSenseMeditation >= 70 ? "Yes" : "No";
        }

        protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
        {
            Mindwave.Current.CurrentValueChanged -= Current_CurrentValueChanged;
        }
    }
}